<template>
	<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item prop="userName">
				<el-input v-model="state.queryForm.userName" placeholder="用户名"></el-input>
			</el-form-item>
			<el-form-item prop="nickName">
				<el-input v-model="state.queryForm.nickName" placeholder="用户昵称"></el-input>
			</el-form-item>
			<el-form-item prop="createdTime">
				<el-date-picker v-model="state.queryForm.createdTime" type="daterange" range-separator="-"
					start-placeholder="注册时间从" end-placeholder="至" value-format="YYYY-MM-DD HH:mm:ss" />
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table">
			<el-table-column prop="headImageThumb" label="用户头像" header-align="center" align="center">
				<template #default="scope">
					<el-avatar shape="square" :size="35" :src="scope.row.headImageThumb"></el-avatar>
				</template>
			</el-table-column>
			<el-table-column prop="userName" label="用户名" header-align="center" align="center"></el-table-column>
			<el-table-column prop="nickName" label="昵称" header-align="center" align="center"></el-table-column>
			<ma-dict-column prop="sex" label="性别" dict-type="user_gender" align="center"></ma-dict-column>
			<el-table-column prop="createdTime" label="注册时间" header-align="center" align="center"></el-table-column>
			<el-table-column prop="lastLoginTime" label="最后登录时间" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-button v-auth="'im:user:info'" type="primary" link
						@click="detailHandle(scope.row.id)">详情</el-button>
					<el-button v-auth="'im:user:ban'" v-show="!scope.row.isBanned" type="danger" link
						@click="banHandle(scope.row)">封禁</el-button>
					<el-button v-auth="'im:user:ban'" v-show="scope.row.isBanned" type="primary" link
						@click="unbanHandle(scope.row)">解封</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination :current-page="state.page" :page-sizes="state.pageSizes" :page-size="state.limit"
			:total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="sizeChangeHandle"
			@current-change="currentChangeHandle">
		</el-pagination>
		<!-- 详情 -->
		<detail ref="detailRef"></detail>
	</el-card>
</template>

<script setup lang="ts" name="ImUserIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserBanApi, useUserUnbanApi } from '@/api/im/user'
import Detail from './detail.vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/im/user/page',
	queryForm: {
		userName: '',
		nickName: '',
		createdTime: ''
	}
})

const queryRef = ref()
const detailRef = ref()
const detailHandle = (id: number) => {
	detailRef.value.init(id)
}

const banHandle = (user: any) => {
	ElMessageBox.prompt('封禁原因:', '确定对该用户进行封禁？', {
		inputPattern: /[A-Za-z0-9]+/,
		inputErrorMessage: '请输入封禁原因',
		confirmButtonText: '确定',
		cancelButtonText: '取消'
	}).then(({ value }) => {
		const data = { id: user.id, reason: value }
		useUserBanApi(data).then(() => {
			user.isBanned = true
			ElMessage.success(`用户'${user.userName}'已被封禁`);
		})
	})
}

const unbanHandle = (user: any) => {
	ElMessageBox.confirm('确定解除该用户的封禁状态？？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消'
	}).then(() => {
		const data = { id: user.id }
		useUserUnbanApi(data).then(() => {
			user.isBanned = false
			ElMessage.success(`用户'${user.userName}'解锁成功`);
		})

	})
}

const { getDataList, sizeChangeHandle, currentChangeHandle, reset } = useCrud(state)
</script>
